package service.impl;

import mapper.ErrorCodeApplyMapper;
import mapper.LeaveApplyMapper;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import po.ErrorCodeApply;
import po.LeaveApply;
import service.ErrorCodeService;
import service.LeaveService;

import java.util.*;

@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT,timeout=5)
@Service
public class ErrorCodeServiceImpl implements ErrorCodeService {
    @Autowired
    ErrorCodeApplyMapper errorCodeApplyMapper;
    @Autowired
    IdentityService identityservice;
    @Autowired
    RuntimeService runtimeservice;
    @Autowired
    TaskService taskservice;

    public ProcessInstance startWorkflow(ErrorCodeApply apply, String userid, Map<String, Object> variables) {
        apply.setUser_id(userid);
        errorCodeApplyMapper.save(apply);
        String businesskey = String.valueOf(apply.getId());//使用leaveapply表的主键作为businesskey,连接业务数据和流程数据
        identityservice.setAuthenticatedUserId(userid);
        ProcessInstance instance = runtimeservice.startProcessInstanceByKey("ticket", businesskey, variables);
        System.out.println(businesskey);
        String instanceid = instance.getId();
        apply.setProcess_instance_id(instanceid);
        errorCodeApplyMapper.update(apply);
        return instance;
    }

    public ErrorCodeApply getErrorcode(int id) {
        ErrorCodeApply leave = errorCodeApplyMapper.get(id);
        return leave;
    }
}
